﻿using System.Collections.Generic;
using FollowMe.Services.Response;
using FollowMe.Model;

namespace FollowMe.Services.Mappers
{
    public static class MemberExtensionMethods
    {
        public static MemberView ConvertToMemberView(this Member member)
        {
            return new MemberView
            {
                firstName = member.FirstName,
                middleInitial = member.MiddleInitial,
                lastName = member.LastName,
                memberId = member.MemberId,
                email = member.Email,
                userName = member.UserName,
                willShareLocation = member.WillShareLocation,
                messages = GenerateMessageViewsFrom(member.MessagesSent),
                friends = InitializeFriendViewsFrom(member.FriendsIAdded),
                registrationIDs = GenerateRegistrationIDViewsFrom(member.RegistrationIDs),
                locations = GenerateLocationViewsFrom(member.Locations)
            };
        }

        public static IList<MemberView> ConvertToMemberViewsFrom(this IEnumerable<Member> members)
        {
            IList<MemberView> memberViews = new List<MemberView>();
            foreach (Member member in members)
            {
                memberViews.Add(member.ConvertToMemberView());
            }

            return memberViews;
        }

        private static IList<MessageView> GenerateMessageViewsFrom(IEnumerable<Message> messages)
        {
            if (messages == null)
                return new List<MessageView>();
            else
                return messages.ConvertToMessageViews();
        }

        public static IList<LocationView> GenerateLocationViewsFrom(this IEnumerable<Location> locations)
        {
            if (locations == null)
                return new List<LocationView>();
            else
                return locations.ConvertToLocationViews();
        }

        public static IList<RegistrationIDView> GenerateRegistrationIDViewsFrom(this IEnumerable<RegistrationID> registrationIDs)
        {
            if (registrationIDs == null)
                return new List<RegistrationIDView>();
            else
                return registrationIDs.ConvertToRegistrationIDViews();
        }

        public static IList<FriendView> GenerateFriendViewsFrom(this IEnumerable<Member> friends)
        {
            if (friends == null)
                return new List<FriendView>();
            else
                return friends.ConvertToFriendViews();
        }

        public static IList<FriendView> InitializeFriendViewsFrom(this IEnumerable<Friend> friends)
        {
            if (friends == null)
                return new List<FriendView>();
            else
                return friends.initializeFriendViews();
        }
    }
}
